GameGlobal.canvas = createFirstCanvas();

if (typeof window == "undefined") {
    GameGlobal.window = {};
    GameGlobal.document = {};

    GameGlobal.performance = {
        _instance: wx.getPerformance(),
        now: function () {
            return this._instance.now() / 1000;
        }
    }
}

window.innerWidth = canvas.width;
window.innerHeight = canvas.height;

document.createElement = function (tagName) {
    let result = null;

    switch (tagName) {
        case "canvas":
            result = wx.createCanvas();
            break;

        case "img":
            result = wx.createImage();
            break;

        case "audio":
            result = wx.createInnerAudioContext({
                useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
            });

            Object.defineProperty(result, "currentTime", {
                set(value) {
                    this.seek(value);
                }
            });

            break;

        default:
            break;
    }

    return result;
}

function createFirstCanvas() {
    let firstCanvas = wx.createCanvas();

    firstCanvas.addEventListener = function (type, listener) {
        switch (type) {
            case "touchstart":
                wx.onTouchStart(listener);
                break;

            case "touchmove":
                wx.onTouchMove(listener);
                break;

            case "touchend":
                wx.onTouchEnd(listener);
                break;
        }

    }

    firstCanvas.removeEventListener = function (type, listener) {
        switch (type) {
            case "touchstart":
                wx.offTouchStart(listener);
                break;

            case "touchmove":
                wx.offTouchMove(listener);
                break;

            case "touchend":
                wx.offTouchEnd(listener);
                break;
        }
    }

    return firstCanvas;
}